import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import LoginView from "../views/LoginView.vue";
import IndexView from "../views/home/IndexView.vue";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "login",
    component: LoginView,
  },
  {
    path: "/home",
    name: "home",
    component: IndexView,
    children: [
      {
        path: "/admin",
        name: "admin",
        meta: {
          title: "管理员管理",
        },
        component: () => import("@/views/home/AdminView.vue"),
      },
      {
        path: "/user",
        name: "user",
        meta: {
          title: "人员管理",
        },
        component: () => import("@/views/home/UserView.vue"),
      },
      {
        path: "/home",
        name: "home",
        meta: {
          title: "首页",
        },
        component: () => import("@/views/home/HomeView.vue"),
      },
      {
        path: "/group",
        name: "group",
        meta: {
          title: "部门管理",
        },
        component: () => import("@/views/home/GroupView.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

// 全局前置路由守卫
// router.beforeEach((to, from, next) => {
//   // 判断有 token
//   if (sessionStorage.token) {
//     // 要去的页面如果登录页，直接跳转到首页
//     if (to.path == "/") return next("/home");
//     // 如果不是登录页，直接放行
//     next();
//     // 没有 token
//   } else {
//     // 如果要去登录页，放行
//     if (to.path == "/") return next();
//     // 否则，强制跳转到登录页
//     next("/");
//   }
// });

export default router;
